package com.yanfan.njLighting.service.impl;

import java.util.Arrays;
import java.util.List;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.njLighting.utils.ModbusSender;
import com.yanfan.njLighting.utils.OneOffsetQuery;
import com.yanfan.njLighting.utils.TenToSixteenUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanfan.njLighting.mapper.NjLightingDeviceStatusMapper;
import com.yanfan.njLighting.domain.NjLightingDeviceStatus;
import com.yanfan.njLighting.service.INjLightingDeviceStatusService;

/**
 * 开关状态列Service业务层处理
 *
 * @author genluo
 * @date 2024-10-30
 */
@Service
public class NjLightingDeviceStatusServiceImpl extends ServiceImpl<NjLightingDeviceStatusMapper, NjLightingDeviceStatus> implements INjLightingDeviceStatusService {
    @Autowired
    private NjLightingDeviceStatusMapper njLightingDeviceStatusMapper;

    @Value("${api.lighting.ip}")
    private String lightingIp;

    @Value("${api.lighting.port}")
    private Integer port;


    /**
     * 查询开关状态列列表
     *
     * @param njLightingDeviceStatus 开关状态列
     * @return 开关状态列
     */
    @Override
    public List<NjLightingDeviceStatus> queryPageList(NjLightingDeviceStatus njLightingDeviceStatus) {
        LambdaQueryWrapper<NjLightingDeviceStatus> lqw = buildQueryWrapper(njLightingDeviceStatus);
        return njLightingDeviceStatusMapper.selectList(lqw);
        //连表查询可用该方法，在此方法SQL语句上进行扩展
        //return njLightingDeviceStatusMapper.queryPageList(njLightingDeviceStatus);
    }

    /**
     * 开关控制
     * @param lightingDeviceStatus
     * @return
     */
    @Override
    public AjaxResult updateByStatus(NjLightingDeviceStatus lightingDeviceStatus) {
        //为0执行关操作
        NjLightingDeviceStatus deviceStatus = baseMapper.selectOne(new LambdaQueryWrapper<NjLightingDeviceStatus>()
                .eq(NjLightingDeviceStatus::getDeviceOffset,
                        lightingDeviceStatus.getDeviceOffset()));
        AjaxResult ajaxResult = new AjaxResult();
        if (lightingDeviceStatus.getNewStatus() == 0){
            //为0时，关灯操作
            byte[] command = TenToSixteenUtils.replaceAddressInCommand(Integer
                            .parseInt(deviceStatus.getDeviceOffset()));
            try {
                // 发送 Modbus 指令并接收响应
                byte[] response = ModbusSender.sendCustomModbusCommand(lightingIp, port, command);

                // 检查指令是否成功
                if (!response.equals(null)) {
                    String status = OneOffsetQuery.getLightingStatus(deviceStatus, lightingIp, port);
                    deviceStatus.setStatus(Integer.valueOf(status));
                    int i = baseMapper.updateById(deviceStatus);
                    ajaxResult.put("msg","关闭成功！");
                } else {
                    ajaxResult.put("msg","关闭失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                ajaxResult.put("msg","关闭失败");
            }
        }else {
            //为401时，开灯
            byte[] command = TenToSixteenUtils.openStatus(Integer.
                            parseInt(deviceStatus.getDeviceOffset()));
            try {
                // 发送 Modbus 指令并接收响应
                byte[] response = ModbusSender.sendCustomModbusCommand(lightingIp, port, command);
                // 检查指令是否成功
                if (!response.equals(null)) {
                    String status = OneOffsetQuery.getLightingStatus(deviceStatus, lightingIp, port);
                    deviceStatus.setStatus(Integer.valueOf(status));
                    int i = baseMapper.updateById(deviceStatus);
                    ajaxResult.put("msg","打开成功！");
                } else {
                    ajaxResult.put("msg","打开失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                ajaxResult.put("msg","打开失败");
            }
        }
        return ajaxResult;
    }

    /**
    * 分页查询条件
    * @return
    */
    private LambdaQueryWrapper<NjLightingDeviceStatus> buildQueryWrapper(NjLightingDeviceStatus bo) {
        LambdaQueryWrapper<NjLightingDeviceStatus> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDeviceType()), NjLightingDeviceStatus::getDeviceType, bo.getDeviceType());
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), NjLightingDeviceStatus::getDeviceName, bo.getDeviceName());
        lqw.like(StringUtils.isNotBlank(bo.getPosition()), NjLightingDeviceStatus::getPosition, bo.getPosition());
        lqw.like(StringUtils.isNotBlank(bo.getAddressIp()), NjLightingDeviceStatus::getAddressIp, bo.getAddressIp());
        lqw.eq(StringUtils.isNotNull(bo.getStatus()), NjLightingDeviceStatus::getStatus, bo.getStatus());
        lqw.orderByAsc(NjLightingDeviceStatus::getDeviceOffset);
        return lqw;
    }



}
